<%--***************************************
*
*PayPal
* 
***************************************--%>
<%@ page import="
	java.util.*,
	java.net.*,
	java.io.*,
	java.math.BigDecimal,
	org.apache.commons.lang.StringUtils,
	com.probiz.estore.Constants,
	com.probiz.estore.system.util.TranslateService,
	com.probiz.estore.order.model.OnlinePaymentResponse,
	com.probiz.estore.common.model.system.PaymentGateway,
	com.probiz.estore.system.service.PaymentGatewayManager,
	com.probiz.estore.common.service.OrderService,
	org.springframework.web.context.support.WebApplicationContextUtils,
	org.springframework.web.context.WebApplicationContext"
	pageEncoding="UTF-8"%>
<%
	//response.setCharacterEncoding("UTF-8");
	WebApplicationContext webAppContext = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
	PaymentGatewayManager paymentGatewayManager = (PaymentGatewayManager) webAppContext.getBean("paymentGatewayManager");
	PaymentGateway paymentGateway = paymentGatewayManager.getByPaymentGatewayCode("paypal_china");
	
	String orderNo=request.getParameter("custom");  //store the order no in custom parameter when commit payment reuqest
	String mc_gross = request.getParameter("mc_gross");				//支付金额
	String txn_id = request.getParameter("txn_id");					//流水号
	String receiver_email = request.getParameter("receiver_email"); //支付人邮件
	BigDecimal total_fee = null;
	if (StringUtils.isNotBlank(mc_gross)) {
		total_fee = new BigDecimal(mc_gross);				
	}
	String type=paymentGateway.getConfigData().get("type").toString();
	String requestUrl="";			//提交地址
	if(type.equals("1")){
		requestUrl="https://www.paypal.com/cgi-bin/webscr";
	}else{
		requestUrl="https://www.sandbox.paypal.com/cgi-bin/webscr";
	}
	
	String payment_status=request.getParameter("payment_status");
	Short flag = Constants.PAYMENT_HISTORY_FLAG_FAILURE;

	if(payment_status!=null && payment_status.equalsIgnoreCase("Completed")){
		// 从 PayPal 出读取 POST 信息同时添加变量cmd
		Enumeration en = request.getParameterNames();
		String str = "cmd=_notify-validate";
		while(en.hasMoreElements()){
		   String paramName = (String)en.nextElement();
		   String paramValue = request.getParameter(paramName);
		   str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue);
		}
		System.out.println(str);
		URL u = new URL(requestUrl);
		URLConnection uc = u.openConnection();
		uc.setDoOutput(true); 
		uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		PrintWriter pw = new PrintWriter(uc.getOutputStream());
		pw.println(str);
		pw.close();
		//接受 PayPal 对 IPN 回发的回复信息
		BufferedReader in= new BufferedReader(new InputStreamReader(uc.getInputStream()));
		String responseText = in.readLine();
		in.close(); 
		
		if(responseText.trim().equalsIgnoreCase("VERIFIED")){
			//the data come from paypal
			flag = Constants.PAYMENT_HISTORY_FLAG_SUCCESS;
		}else if(responseText.trim().equalsIgnoreCase("INVALID")){
			//Declined (invalid request)
			//first_name last_name 不为英文会乱码 ，导致验证不通过
			System.out.println("!Not VERIFIED . it may be first_name or last_name is not english"); 
			flag = Constants.PAYMENT_HISTORY_FLAG_FAILURE;
		}else if("Pending".equals(payment_status)){
			//Queued
			flag = Constants.PAYMENT_HISTORY_FLAG_WAIT;
		}
		
		OnlinePaymentResponse onlinePaymentResponse = new OnlinePaymentResponse();
		onlinePaymentResponse.setFlowNo(txn_id);   //必填. 如果支付网关没有流水号，则使用原始的orderNo，即未decode前的
		onlinePaymentResponse.setOrderNo(orderNo);  //必填
		onlinePaymentResponse.setFlag(flag);    //必填
		onlinePaymentResponse.setAmount(total_fee);  //必填
		onlinePaymentResponse.setPaymentGateway(paymentGateway);  //必填
		onlinePaymentResponse.setPayer(receiver_email);
		OrderService orderService = (OrderService) webAppContext.getBean("orderService");
		request.setAttribute("onlinePaymentResponse",onlinePaymentResponse);
		orderService.doHandleOnlinePaymentResponse(onlinePaymentResponse,request);
	}	
%>
<%@include file="./include/finishPayResult.jspf"%>
